#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

[ExecuteInEditMode]
public class CameraFrustumEditor : MonoBehaviour
{
    public Camera targetCamera;
    public Color frustumColor = Color.yellow;
    public bool drawNearPlane = true;
    public bool drawFarPlane = true;
    public bool drawConnectingLines = true;

    void OnDrawGizmos()
    {
#if UNITY_EDITOR
        if (targetCamera == null)
            targetCamera = GetComponent<Camera>();

        if (targetCamera == null)
            return;

        Handles.color = frustumColor;

        Vector3[] nearCorners = new Vector3[4];
        Vector3[] farCorners = new Vector3[4];

        // 获取视锥体角点（支持正交和透视投影）
        if (targetCamera.orthographic)
        {
            // 正交相机处理
            float orthoWidth = targetCamera.orthographicSize * targetCamera.aspect;
            float orthoHeight = targetCamera.orthographicSize;

            // 近平面角点
            nearCorners[0] = new Vector3(-orthoWidth, -orthoHeight, targetCamera.nearClipPlane);
            nearCorners[1] = new Vector3(orthoWidth, -orthoHeight, targetCamera.nearClipPlane);
            nearCorners[2] = new Vector3(orthoWidth, orthoHeight, targetCamera.nearClipPlane);
            nearCorners[3] = new Vector3(-orthoWidth, orthoHeight, targetCamera.nearClipPlane);

            // 远平面角点
            farCorners[0] = new Vector3(-orthoWidth, -orthoHeight, targetCamera.farClipPlane);
            farCorners[1] = new Vector3(orthoWidth, -orthoHeight, targetCamera.farClipPlane);
            farCorners[2] = new Vector3(orthoWidth, orthoHeight, targetCamera.farClipPlane);
            farCorners[3] = new Vector3(-orthoWidth, orthoHeight, targetCamera.farClipPlane);
        }
        else
        {
            // 透视相机处理
            targetCamera.CalculateFrustumCorners(
                new Rect(0, 0, 1, 1),
                targetCamera.nearClipPlane,
                Camera.MonoOrStereoscopicEye.Mono,
                nearCorners);

            targetCamera.CalculateFrustumCorners(
                new Rect(0, 0, 1, 1),
                targetCamera.farClipPlane,
                Camera.MonoOrStereoscopicEye.Mono,
                farCorners);
        }

        // 转换为世界坐标
        for (int i = 0; i < 4; i++)
        {
            nearCorners[i] = targetCamera.transform.TransformPoint(nearCorners[i]);
            farCorners[i] = targetCamera.transform.TransformPoint(farCorners[i]);
        }

        // 绘制近平面
        if (drawNearPlane)
        {
            Handles.DrawLine(nearCorners[0], nearCorners[1]);
            Handles.DrawLine(nearCorners[1], nearCorners[2]);
            Handles.DrawLine(nearCorners[2], nearCorners[3]);
            Handles.DrawLine(nearCorners[3], nearCorners[0]);
        }

        // 绘制远平面（修正后的顺序）
        if (drawFarPlane)
        {
            Handles.DrawLine(farCorners[0], farCorners[1]);
            Handles.DrawLine(farCorners[1], farCorners[2]);
            Handles.DrawLine(farCorners[2], farCorners[3]);
            Handles.DrawLine(farCorners[3], farCorners[0]);
        }

        // 绘制连接线
        if (drawConnectingLines)
        {
            Handles.DrawLine(nearCorners[0], farCorners[0]);
            Handles.DrawLine(nearCorners[1], farCorners[1]);
            Handles.DrawLine(nearCorners[2], farCorners[2]);
            Handles.DrawLine(nearCorners[3], farCorners[3]);
        }
#endif
    }
}